import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { getToken } from '@/utils/auth'
import { ElMessage } from 'element-plus'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({ showSpinner: false })

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/login/index.vue'),
      hidden: true
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('@/views/register/index.vue'),
      hidden: true
    },
    {
      path: '/',
      component: () => import('@/layout/index.vue'),
      redirect: '/dashboard',
      children: [
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: () => import('@/views/dashboard/index.vue'),
          meta: { title: '首页', icon: 'dashboard', affix: true }
        }
      ]
    },
    {
      path: '/system',
      component: () => import('@/layout/index.vue'),
      redirect: '/system/user',
      name: 'System',
      meta: { title: '系统管理', icon: 'system' },
      alwaysShow: true,
      children: [
        {
          path: 'user',
          name: 'User',
          component: () => import('@/views/system/user/index.vue'),
          meta: { title: '用户管理', icon: 'user' }
        },
        {
          path: 'admin',
          name: 'Admin',
          component: () => import('@/views/system/admin/index.vue'),
          meta: { title: '管理员管理', icon: 'peoples' }
        },
        {
          path: 'role',
          name: 'Role',
          component: () => import('@/views/system/role/index.vue'),
          meta: { title: '角色管理', icon: 'peoples' }
        }
      ]
    },
    {
      path: '/content',
      component: () => import('@/layout/index.vue'),
      redirect: '/content/article',
      name: 'Content',
      meta: { title: '内容管理', icon: 'edit' },
      alwaysShow: true,
      children: [
        {
          path: 'article',
          name: 'Article',
          component: () => import('@/views/content/article/index.vue'),
          meta: { title: '文章管理', icon: 'edit' }
        },
        {
          path: 'category',
          name: 'Category',
          component: () => import('@/views/content/category/index.vue'),
          meta: { title: '分类管理', icon: 'tree' }
        },
        {
          path: 'tag',
          name: 'Tag',
          component: () => import('@/views/content/tag/index.vue'),
          meta: { title: '标签管理', icon: 'price-tag' }
        },
        {
          path: 'comment',
          name: 'Comment',
          component: () => import('@/views/content/comment/index.vue'),
          meta: { title: '评论管理', icon: 'message' }
        }
      ]
    },
    {
      path: '/member',
      component: () => import('@/layout/index.vue'),
      redirect: '/member/index',
      name: 'Member',
      meta: { title: '会员管理', icon: 'star' },
      alwaysShow: true,
      children: [
        {
          path: 'index',
          name: 'MemberIndex',
          component: () => import('@/views/member/index.vue'),
          meta: { title: '会员管理', icon: 'star' }
        },
        {
          path: 'level',
          name: 'MemberLevel',
          component: () => import('@/views/member/level/index.vue'),
          meta: { title: '会员等级', icon: 'star' }
        }
      ]
    }
  ]
})

const whiteList = ['/login', '/register']

router.beforeEach(async (to, from, next) => {
  NProgress.start()
  
  const hasToken = getToken()
  
  if (hasToken) {
    if (to.path === '/login') {
      next({ path: '/' })
      NProgress.done()
    } else {
      // 简化权限验证，直接放行所有已登录用户
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})

export default router
